﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Reflection;

namespace ReportLib
{
    [DataContract]
    public class ReportDetails : ReportSummary
    {
        List<ReportParameter> _parameters = new List<ReportParameter>();

        [DataMember]
        public List<ReportParameter> Parameters
        {
            get { return _parameters; }
            set { _parameters = value; }
        }

        public void PopulateParameters(object entity)
        {
            if (entity == null)
                return;
            Parameters.ForEach(x =>
            {
                try
                {
                   PropertyInfo pinfo= entity.GetType().GetProperty(x.Name) ;
                   if (pinfo != null)
                   {
                       x.Value= pinfo.GetValue(entity, null).ToString();
                   }
                }
                catch
                { }
            });  
        }

        public string ToWebString()
        {
            StringBuilder retString = new StringBuilder(string.Format("FolderName={0}&ReportName={1}", this.FolderName, this.ReportName));
            
            Parameters.ForEach(x => 
            {
                retString.Append(string.Format("&{0}={1}", x.Name, x.Value));
            });

            return retString.ToString();
        }
    }
}
